//
//  FPProtocolListController.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/2/17.
//

#import "FPProtocolListController.h"

@interface FPProtocolListController () <UITableViewDelegate , UITableViewDataSource>

@property (nonatomic , strong) NSMutableArray *protocolSource;

@end

@implementation FPProtocolListController

LazyLoadDataSource(protocolSource);

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)initUI {
    
    [self setTitle:@"我的协议" style:(XYCustomNavigationStyleClear) hiddenBackBtn:NO];
    
    self.view.backgroundColor = MAIN_VIEW_BACKGROUND_COLOR;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:self.tableView];
    self.tableView.frame = CGRectMake(0.f, UIDevice.navigationFullHeight, SCREEN_WIDTH, SCREEN_HEIGHT - UIDevice.navigationFullHeight);
    self.tableView.backgroundColor = UIColor.clearColor;
    self.tableView.rowHeight = 50.f;
    
    [self getProtocol];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.protocolSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FPStandardLabelCell *cell;
    
    if (!cell) {
        
        cell = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(FPStandardLabelCell.class) owner:nil options:nil].lastObject;
    }
    
    cell.backView.backgroundColor = UIColor.whiteColor;
    
    CGRect bounds = CGRectMake(0.f, 0.f, SCREEN_WIDTH - 32.f, 50.f);
    
    if (indexPath.row == 0) {
        
        [UIView setCornerForView:cell.backView withRadius:5.f bounds:bounds corners:(UIRectCornerTopLeft | UIRectCornerTopRight)];
    }
    else if (indexPath.row == self.protocolSource.count - 1) {
        
        [UIView setCornerForView:cell.backView withRadius:5.f bounds:bounds corners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)];
        cell.lineView.hidden = YES;
    }
    
    FPProtocolModel *model = self.protocolSource[indexPath.row];
    
    cell.mainLab.text = model.agreementName;
    
    return cell;
}

#pragma mark - 获取协议
- (void)getProtocol {
    
    [MBManager showLoading];
    [FPProtocolModel getProtocolWithType:(FPProtocolTypeListController) success:^(NSInteger code, NSString * _Nonnull message, NSArray *list) {
        [MBManager hideLoading];
        
        [self.protocolSource removeAllObjects];
        if (code == 200) {
            
            for (FPProtocolModel *model in list) {
                
                [self.protocolSource addObject:model];
            }
        }
        
        [self.tableView reloadData];
        
    } failure:^(NSError * _Nonnull error) {
        [MBManager hideLoading];
    }];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FPProtocolModel *model = self.protocolSource[indexPath.row];
    
    FPMainWebController *vc = [[FPMainWebController alloc] init];
    
    vc.model = model;
    vc.userPanGestureInvalid = NO;
    vc.protocolType = FPProtocolTypeListController;
    
    [self.navigationController pushViewController:vc animated:YES];
}

@end
